package peng.yang.express.VolleyUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2016/6/28.
 */
public class BitmapUtil {

    Context context;

    private static BitmapUtil util;
    Bitmap bitmap = null;

    public BitmapUtil(Context context) {
        this.context = context;
    }

    public void savePicture(String url,String path) {

        URL myFileUrl = null;
        try {
            myFileUrl = new URL(url);
            Log.e("BitmapUtileurl",url);
            HttpURLConnection connection= (HttpURLConnection) myFileUrl.openConnection();

            connection.connect();

            if (connection.getResponseCode() == 200) {

                InputStream stream = connection.getInputStream();//拿到网络流数据
                // String  filePath = getFilesDir()+"yuantong.jpg";

                File file = new File(context.getFilesDir(),path);
                OutputStream putStream = new FileOutputStream(file);
                int length = -1;
                while ((length = stream.read()) != -1) {
                    putStream.write(length);

                }
                putStream.close();
                stream.close();

            } else {
                Log.e("=========", "网络请求失败");
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Bitmap openPicture(String path) {
       // String pictureName = "/mnt/sdcard/" + "car" + ".jpg";

        try {
            InputStream is = context.openFileInput(path);
            Bitmap bitmap = BitmapFactory.decodeStream(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return  bitmap;



     /*   File file = new File(path);
        FileInputStream inputStream;
        try {
            inputStream = new FileInputStream(file);
            Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;*/
    }

}
